import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
// 引入用户相关的仓库
import useUserStore from '@/store/modules/user'
import { errorHandle } from './errorHandle'
import { GET_TOKEN } from '@/utils/token'

// 创建 Axios 实例的类
class HttpRequest {
     private instance: AxiosInstance
     constructor(config: AxiosRequestConfig) {
          this.instance = axios.create(config) //创建axios实例
          this.setupInterceptors()
     }

     // 设置拦截器
     private setupInterceptors() {
          this.instance.interceptors.request.use((config) => {
               const userStore = useUserStore()
               if (userStore.token) {
                    config.headers.TokenValue = GET_TOKEN()
               }
               return config
          })

          this.instance.interceptors.response.use(
               (response) => {
                    return response.data
               },
               (error) => {
                    const response = error.response
                    if (response) {
                         errorHandle(response.status)
                    }
                    return Promise.reject(error)
               },
          )
     }

     // 动态设置 baseURL
     public setBaseUrl(baseURL: string) {
          this.instance.defaults.baseURL = baseURL
     }

     // 上传文件的方法
     public upload<T = any, R = AxiosResponse<T>, D = any>(
          config: AxiosRequestConfig<D>,
          files: any,
          isSplice?: boolean,
          isArray?: boolean,
     ): Promise<R> {
          const { url, data, responseType = 'json' }: any = config // 默认响应类型为 json
          let fullUrl = `${url}`
          const queryString = new URLSearchParams(data).toString()
          const formData = new FormData()
          if (isSplice) {
               fullUrl = `${url}?${queryString}`
          } else {
               for (const key in data) {
                    formData.append(key, data[key])
               }
          }
          files?.forEach((item: any) => {
               if (isArray) {
                    formData.append('files', item)
               } else {
                    formData.append('file', item)
               }
          })
          return this.instance({
               ...config,
               url: fullUrl,
               data: formData,
               method: 'POST',
          })
     }

     // 其他 HTTP 方法 (GET, POST, PUT, DELETE 等)
     public get<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
          return this.instance.get(url, config)
     }

     public post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
          return this.instance.post(url, data, config)
     }

     public put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
          return this.instance.put(url, data, config)
     }

     public delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
          return this.instance.delete(url, config)
     }
}
export default HttpRequest
